﻿using Sys.Common;
using Sys.Core.Sys;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Api.Sys
{
    public class DictionaryApi : BaseApi
    {
        //public string GetNameByID(string id)
        //{
        //    string result = string.Empty;
        //    var entity = DictionaryBll.I.GetByKey(id);
        //    if (entity != null)
        //        result = entity.Name;
        //    return result;
        //}

        /// <summary>
        /// 列表（根据类型的ID进行检索）
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public string Grid(string dic)
        {
            var entities = DictionaryBll.I.SortEntities;

            entities = entities.Where(m => m.DicTypeID.Equals(dic));

            return JsonHelper.ToJson(entities.ToList());
        }

        /// <summary>
        /// 列表（根据类型的Tag进行检索）
        /// </summary>
        /// <returns></returns>
        public string List()
        {
            string tag = HttpContext.Current.Request.QueryString["tag"];

            var entities = DictionaryBll.I.EntitiesByTag(tag).Where(m => m.IsVisible);

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));

            return CustomJson(entities.ToList(), jms, false);
        }

        /// <summary>
        /// 树表（根据类型的Tag进行检索）
        /// </summary>
        /// <returns></returns>
        public string Tree()
        {
            string tag = HttpContext.Current.Request.QueryString["tag"];

            var entities = DictionaryBll.I.EntitiesByTag(tag).Where(m => m.IsVisible);

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));

            return CustomJson(entities.ToList(), jms, false);
        }

        public string FilterTree()
        {
            string tag = HttpContext.Current.Request.QueryString["tag"];

            var list = DictionaryBll.I.EntitiesByTag(tag).Where(m => m.IsVisible).ToList();

            var model = DictionaryBll.I.NullModel();

            model.Name = "查看所有";

            list.Insert(0, model);

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));

            return CustomJson(list, jms, false);
        }

        public string ComboTree()
        {
            string dic = HttpContext.Current.Request.QueryString["dic"];

            var entities = DictionaryBll.I.SortEntities;

            entities = entities.Where(m => m.DicTypeID.Equals(dic));

            var list = entities.ToList();
            list.Insert(0, DictionaryBll.I.NullModel());

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));

            return CustomJson(list, jms, false);
        }

        public string Save(SysDictionary model)
        {
            if (model.Parent.Equals("Null")) { model.Parent = string.Empty; }

            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.IntSort = DictionaryBll.I.MaxSort(m => m.IntSort);

                result.Success = DictionaryBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                result.Success = DictionaryBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = DictionaryBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            SysDictionary model = DictionaryBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = DictionaryBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

        public string SetOrder(string id, string parent, string dic, string order)
        {
            JsonMsg result = new JsonMsg();

            var entities = DictionaryBll.I.Entities.Where(m => m.Parent.Equals(parent) && m.DicTypeID.Equals(dic));

            bool success = false;

            result.Message = DictionaryBll.I.SetOrder(entities, id, "IntSort", order, out success);

            result.Success = success;

            return JsonHelper.ToJson(result);
        }
    }
}
